/*
 * @author java_deng
 * @date 2024/11/29 15:41
 * @description
 */
package org.hbwl.configuration;

import cn.hutool.json.JSONUtil;
import org.hbwl.utils.JwtUtil;
import org.hbwl.utils.ResponseData;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String authorization = request.getHeader("Authorization");
        Boolean isLogin= JwtUtil.parseJWT(authorization);
        if (!isLogin) {
            response.setContentType("Application/json;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.write(JSONUtil.toJsonStr(new ResponseData("401","请先登录","")));
            writer.close();
            writer.flush();
            return false;
        }
        return true;
    }
}
